import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/main',
        component: () => import(/* webpackChunkName: "main" */ '../views/main/home.vue'),
        children:[
            {
                path:'/main/music',
                component:() => import(/* webpackChunkName: "main" */ '../views/main/music.vue'),
            },
            {
                path:'/main/my',
                component:() => import(/* webpackChunkName: "main" */ '../views/main/my.vue'),
            }
        ],
        redirect:'/main/music'
    },
    {
        path: '/login',
        component: () => import(/* webpackChunkName: "login" */ '../views/login.vue')
    },
    {
        path: '/registry',
        component: () => import(/* webpackChunkName: "login" */ '../views/registry.vue')
    },
    {
        path:'/',
        redirect:'/main'
    }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//全局前置守卫   
router.beforeEach((to,from,next) => {
    //守卫除了  登录，注册接口之外的
    let writePath = ['/login','/registry'];
    console.log(to.path)
    if(writePath.includes(to.path)){
        next();
    }else{
        //守卫  1.登录成功   进去
        let token = localStorage.getItem('token');
        if(token){
            next();
        }else{
            next('/login');
        }

    }
})

export default router

// 路由守卫：

//1.全局守卫 ：beforeEach

//2.路由独享守卫  

//3.组件内守卫


